package com.setting.service;

import java.util.ArrayList;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Customer;
import com.entity.Org;
import com.entity.Sysdic;
import com.file.entity.FileCustomer;
import com.file.service.CustomerService;
import com.interfaces.CustomerDao;
import com.interfaces.CustomerMeterDao;
import com.interfaces.OrgDao;
import com.interfaces.*;
import com.setting.entity.JsonOrg;
import com.setting.entity.JsonOrgMenu;
import com.setting.entity.Children;

import com.utils.CglibBeanCopierUtils;
import com.utils.JsonResult;

/**
 * @Title: OrgService.java
 * @Package com.service
 * @author 孙梦楠
 * @date 2017年7月25日 上午10:53:58
 * @version V1.0.0
 */

@Service
public class OrgService {

	@Autowired
	private SysdicDao sysdicdao;

	@Autowired
	private OrgDao orgdao;

	@Autowired
	private SysdicService sysdicservice;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private CustomerService customerservice;

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private ChildMeterDao childmeterdao;

	// 带用户列表
	public JsonResult<List<Children>> list_org_customer(String languagecookie, String opeorgcookie,
			String opetypecookie) {
		if (opeorgcookie.equals("1")) {
			return listallorg(languagecookie, opeorgcookie, opetypecookie);
		} else {
			return listorgcustomer(languagecookie, opeorgcookie, opetypecookie);
		}
	}

	// 架构列表
	public JsonResult<List<Children>> list_org(String languagecookie, String opeorgcookie, String opetypecookie) {
		if (opeorgcookie.equals("1")) {
			return listallorg2(languagecookie, opeorgcookie, opetypecookie);
		} else {
			return listorg(languagecookie, opeorgcookie, opetypecookie);
		}
	}

	// 带用户列表
	public JsonResult<List<Children>> listallorg(String languagecookie, String opeorgcookie, String opetypecookie) {

		List<Org> orgs = orgdao.findByOrgfather("0");
		List<Children> jsonOrgTrees = new ArrayList<Children>();
		for (Org org : orgs) {
			Children jsonOrgTree = new Children();

			List<Org> orgs2 = orgdao.findByOrgfather(org.getOrgvalue());
			List<Children> jsonOrgTrees2 = new ArrayList<Children>();
			for (Org org2 : orgs2) {
				Children jsonOrgTree2 = new Children();

				List<Customer> customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
						Integer.valueOf(org2.getOrgvalue()), opetypecookie, "4");
				List<Children> childrens = new ArrayList<Children>();
				for (Customer customer : customers) {
					Children children = new Children();
					children.setOrgvalue(customer.getCustomerid().toString());
					children.setValuename(customer.getCustomername());
					children.setStatus("2");
					// 只展示分和未定义的用户 ,未连接表的用户
					if (customermeterdao.findByCustomerid2(customer.getCustomerid()) == null
							|| childmeterdao.findByMeterid(customermeterdao.findByCustomerid2(customer.getCustomerid())
									.getPk().getMeterid()) == null
							|| childmeterdao.findByMeterid(
									customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid())
									.getType().equals("2")) {
						childrens.add(children);
					}
				}
				jsonOrgTree2.setStatus("1");
				jsonOrgTree2.setChildren(childrens);
				CglibBeanCopierUtils.copyProperties(org2, jsonOrgTree2);
				jsonOrgTree2.setValuename(sysdicservice.finddisplay("0001", org2.getOrgvalue(), languagecookie));
				jsonOrgTrees2.add(jsonOrgTree2);
			}
			jsonOrgTree.setStatus("1");
			jsonOrgTree.setChildren(jsonOrgTrees2);
			CglibBeanCopierUtils.copyProperties(org, jsonOrgTree);
			jsonOrgTree.setValuename(sysdicservice.finddisplay("0001", org.getOrgvalue(), languagecookie));
			jsonOrgTrees.add(jsonOrgTree);
		}

		return new JsonResult<List<Children>>(157, "展示带用户的组织架构", jsonOrgTrees);
	}

	// 架构列表
	public JsonResult<List<Children>> listallorg2(String languagecookie, String opeorgcookie, String opetypecookie) {

		List<Org> orgs = orgdao.findByOrgfather("0");
		List<Children> jsonOrgTrees = new ArrayList<Children>();
		for (Org org : orgs) {
			Children jsonOrgTree = new Children();

			List<Org> orgs2 = orgdao.findByOrgfather(org.getOrgvalue());
			List<Children> jsonOrgTrees2 = new ArrayList<Children>();
			for (Org org2 : orgs2) {
				Children jsonOrgTree2 = new Children();
				CglibBeanCopierUtils.copyProperties(org2, jsonOrgTree2);
				jsonOrgTree2.setValuename(sysdicservice.finddisplay("0001", org2.getOrgvalue(), languagecookie));
				jsonOrgTrees2.add(jsonOrgTree2);
			}

			jsonOrgTree.setChildren(jsonOrgTrees2);
			CglibBeanCopierUtils.copyProperties(org, jsonOrgTree);
			jsonOrgTree.setValuename(sysdicservice.finddisplay("0001", org.getOrgvalue(), languagecookie));
			jsonOrgTrees.add(jsonOrgTree);
		}

		return new JsonResult<List<Children>>(151, "展示组织架构", jsonOrgTrees);
	}

	// 通过读cookorg展示不同的架构关系：公司层级展示公司及下属小区的，小区层级只展示本小区的......带客户
	public JsonResult<List<Children>> listorgcustomer(String languagecookie, String opeorgcookie,
			String opetypecookie) {
		// 公司层级
		if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
			Org org = orgdao.findByOrgvalue(opeorgcookie);
			List<Children> jsonOrgTrees = new ArrayList<Children>();

			Children jsonOrgTree = new Children();

			List<Org> orgs2 = orgdao.findByOrgfather(org.getOrgvalue());
			List<Children> jsonOrgTrees2 = new ArrayList<Children>();
			for (Org org2 : orgs2) {
				Children jsonOrgTree2 = new Children();

				List<Customer> customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
						Integer.valueOf(org2.getOrgvalue()), opetypecookie, "4");
				List<Children> childrens = new ArrayList<Children>();
				for (Customer customer : customers) {
					Children children = new Children();
					children.setOrgvalue(customer.getCustomerid().toString());
					children.setValuename(customer.getCustomername());
					children.setStatus("2");
					// 只展示分和未定义的用户 ,未连接表的用户
					if (customermeterdao.findByCustomerid2(customer.getCustomerid()) == null
							|| childmeterdao.findByMeterid(customermeterdao.findByCustomerid2(customer.getCustomerid())
									.getPk().getMeterid()) == null
							|| childmeterdao.findByMeterid(
									customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid())
									.getType().equals("2")) {
						childrens.add(children);
					}
				}
				jsonOrgTree2.setStatus("1");
				jsonOrgTree2.setChildren(childrens);
				CglibBeanCopierUtils.copyProperties(org2, jsonOrgTree2);
				jsonOrgTree2.setValuename(sysdicservice.finddisplay("0001", org2.getOrgvalue(), languagecookie));
				jsonOrgTrees2.add(jsonOrgTree2);
			}

			CglibBeanCopierUtils.copyProperties(org, jsonOrgTree);
			jsonOrgTree.setStatus("1");
			jsonOrgTree.setValuename(sysdicservice.finddisplay("0001", org.getOrgvalue(), languagecookie));
			jsonOrgTree.setChildren(jsonOrgTrees2);
			jsonOrgTrees.add(jsonOrgTree);

			return new JsonResult<List<Children>>(157, "展示带用户的组织架构", jsonOrgTrees);
		} else { // 小区层级
			Org org = orgdao.findByOrgvalue(orgdao.findByOrgvalue(opeorgcookie).getOrgfather());
			List<Children> jsonOrgTrees = new ArrayList<Children>();

			Children jsonOrgTree = new Children();

			Org org2 = orgdao.findByOrgvalue(opeorgcookie);
			List<Children> jsonOrgTrees2 = new ArrayList<Children>();

			Children jsonOrgTree2 = new Children();

			List<Customer> customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
					Integer.valueOf(org2.getOrgvalue()), opetypecookie, "4");
			List<Children> childrens = new ArrayList<Children>();
			for (Customer customer : customers) {
				Children children = new Children();
				children.setOrgvalue(customer.getCustomerid().toString());
				children.setValuename(customer.getCustomername());
				children.setStatus("2");
				if (customermeterdao.findByCustomerid2(customer.getCustomerid()) == null
						|| childmeterdao.findByMeterid(customermeterdao.findByCustomerid2(customer.getCustomerid())
								.getPk().getMeterid()) == null
						|| childmeterdao.findByMeterid(
								customermeterdao.findByCustomerid2(customer.getCustomerid()).getPk().getMeterid())
								.getType().equals("2")) {
					childrens.add(children);
				}
			}
			jsonOrgTree2.setStatus("1");
			jsonOrgTree2.setChildren(childrens);
			CglibBeanCopierUtils.copyProperties(org2, jsonOrgTree2);
			jsonOrgTree2.setValuename(sysdicservice.finddisplay("0001", org2.getOrgvalue(), languagecookie));
			jsonOrgTrees2.add(jsonOrgTree2);

			// List<Customer> customers2 =
			// customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
			// Integer.valueOf(org.getOrgvalue()), opetypecookie, "4");
			// jsonOrgTree.setCustomers(customers2);
			CglibBeanCopierUtils.copyProperties(org, jsonOrgTree);
			jsonOrgTree.setStatus("1");
			jsonOrgTree.setValuename(sysdicservice.finddisplay("0001", org.getOrgvalue(), languagecookie));
			jsonOrgTree.setChildren(jsonOrgTrees2);
			jsonOrgTrees.add(jsonOrgTree);

			return new JsonResult<List<Children>>(157, "展示带用户的组织架构", jsonOrgTrees);
		}
	}

	// 通过读cookorg展示不同的架构关系：公司层级展示公司及下属小区的，小区层级只展示本小区的......纯架构
	public JsonResult<List<Children>> listorg(String languagecookie, String opeorgcookie, String opetypecookie) {
		// 公司层级
		if (orgdao.findByOrgvalue(opeorgcookie).getOrgfather().equals("0")) {
			Org org = orgdao.findByOrgvalue(opeorgcookie);
			List<Children> jsonOrgTrees = new ArrayList<Children>();

			Children jsonOrgTree = new Children();

			List<Org> orgs2 = orgdao.findByOrgfather(org.getOrgvalue());
			List<Children> jsonOrgTrees2 = new ArrayList<Children>();
			for (Org org2 : orgs2) {
				Children jsonOrgTree2 = new Children();

				CglibBeanCopierUtils.copyProperties(org2, jsonOrgTree2);
				jsonOrgTree2.setValuename(sysdicservice.finddisplay("0001", org2.getOrgvalue(), languagecookie));
				jsonOrgTrees2.add(jsonOrgTree2);
			}

			CglibBeanCopierUtils.copyProperties(org, jsonOrgTree);
			jsonOrgTree.setValuename(sysdicservice.finddisplay("0001", org.getOrgvalue(), languagecookie));
			jsonOrgTree.setChildren(jsonOrgTrees2);
			jsonOrgTrees.add(jsonOrgTree);

			return new JsonResult<List<Children>>(151, "展示组织架构", jsonOrgTrees);
		} else { // 小区层级
			Org org = orgdao.findByOrgvalue(orgdao.findByOrgvalue(opeorgcookie).getOrgfather());
			List<Children> jsonOrgTrees = new ArrayList<Children>();

			Children jsonOrgTree = new Children();

			Org org2 = orgdao.findByOrgvalue(opeorgcookie);
			List<Children> jsonOrgTrees2 = new ArrayList<Children>();

			Children jsonOrgTree2 = new Children();

			CglibBeanCopierUtils.copyProperties(org2, jsonOrgTree2);
			jsonOrgTree2.setValuename(sysdicservice.finddisplay("0001", org2.getOrgvalue(), languagecookie));
			jsonOrgTrees2.add(jsonOrgTree2);

			CglibBeanCopierUtils.copyProperties(org, jsonOrgTree);
			jsonOrgTree.setValuename(sysdicservice.finddisplay("0001", org.getOrgvalue(), languagecookie));
			jsonOrgTree.setChildren(jsonOrgTrees2);
			jsonOrgTrees.add(jsonOrgTree);

			return new JsonResult<List<Children>>(151, "展示组织架构", jsonOrgTrees);
		}
	}

	// 通过输入架构orgvalue列用户表
	@Transactional
	public JsonResult<List<FileCustomer>> listCustomerByOrg(String orgvalue, String languagecookie, String status,
			String customertype) {

		List<Customer> customers = new ArrayList<Customer>();
		List<FileCustomer> filecustomers = new ArrayList<FileCustomer>();
		if (status.equals("1")) {
			if (orgdao.findByOrgvalue(orgvalue).getOrgfather().equals("0")) {// 公司层级
				List<Org> orgs = orgdao.findByOrgfather(orgvalue);
				for (Org org : orgs) {
					customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
							Integer.parseInt(org.getOrgvalue()), customertype, "4");
					for (Customer customer : customers) {
						FileCustomer fileCustomer = customerservice.dofilecustomer(customer, languagecookie);
						filecustomers.add(fileCustomer);
					}
				}
			} else {// 小区层级
				customers = customerdao.findByCustomerorgAndCustomertypeAndCustomerrechargestatusNot(
						Integer.parseInt(orgvalue), customertype, "4");
				for (Customer customer : customers) {
					FileCustomer fileCustomer = customerservice.dofilecustomer(customer, languagecookie);
					filecustomers.add(fileCustomer);
				}
			}

		} else if (status.equals("2")) {
			Customer customer = customerdao.findOne(Integer.valueOf(orgvalue));
			FileCustomer fileCustomer = customerservice.dofilecustomer(customer, languagecookie);
			filecustomers.add(fileCustomer);

		}
		return new JsonResult<List<FileCustomer>>(158, "通过输入架构orgvalue列用户表", filecustomers);
	}

	// 公司菜单列表
	@Transactional
	public JsonResult<List<JsonOrgMenu>> listcompanymenu(String languagecookie) {

		List<Org> orgs = orgdao.findByOrgfather("0");
		List<JsonOrgMenu> menus = new ArrayList<JsonOrgMenu>();
		if (orgs.size() != 0) {
			for (int i = 0; i < orgs.size(); i++) {

				JsonOrgMenu menu = new JsonOrgMenu();
				String orgvalue = orgs.get(i).getOrgvalue();
				menu.setOrgvalue(orgvalue);
				String orgname = sysdicservice.finddisplay("0001", orgvalue, languagecookie);
				menu.setOrgname(orgname);
				menus.add(menu);
			}
		}
		return new JsonResult<List<JsonOrgMenu>>(153, "公司菜单列表", menus);
	}

	// 小区列表
	@Transactional
	public JsonResult<List<JsonOrgMenu>> listarea(String orgfather, String languagecookie) {
		List<Org> orgs = new ArrayList<Org>();
		// 0的话查公司
		if (orgfather != null && orgfather.equals("0")) {
			orgs = orgdao.findCompanyByConditions();
		} else {
			orgs = orgdao.findAreaByConditions(orgfather);
		}

		List<JsonOrgMenu> menus = new ArrayList<JsonOrgMenu>();
		if (orgs.size() != 0) {
			for (int i = 0; i < orgs.size(); i++) {

				JsonOrgMenu menu = new JsonOrgMenu();
				String orgvalue = orgs.get(i).getOrgvalue();
				menu.setOrgvalue(orgvalue);
				String orgname = sysdicservice.finddisplay("0001", orgvalue, languagecookie);
				menu.setLeadername(getLeadername(orgvalue));
				menu.setPhonenum(getPhonenum(orgvalue));
				menu.setCn_areanum(getCn_areanum(orgvalue));
				menu.setOrgname(orgname);
				if (orgfather == null || !orgfather.equals("0")) {
					menu.setOrgfather(orgs.get(i).getOrgfather());
					menu.setOrgfathername(
							sysdicservice.finddisplay("0001", orgs.get(i).getOrgfather(), languagecookie));
				}
				menus.add(menu);
			}
		}

		return new JsonResult<List<JsonOrgMenu>>(154, "小区列表", menus);

	}

	// 获取负责人
	private String getLeadername(String orgvalue) {
		String remark = sysdicdao.findBySysdictypeAndSysdicvalue("0001", orgvalue).getSysdicremake();
		String[] remarks = remark.split("_");
		String leadrname = remarks[0];
		return leadrname;
	}

	// 获取手机号
	private String getPhonenum(String orgvalue) {
		String remark = sysdicdao.findBySysdictypeAndSysdicvalue("0001", orgvalue).getSysdicremake();
		String[] remarks = remark.split("_");
		String leadrname = remarks[1];
		return leadrname;
	}

	// 获取行政区号
	private String getCn_areanum(String orgvalue) {
		String remark = sysdicdao.findBySysdictypeAndSysdicvalue("0001", orgvalue).getSysdicremake();
		String[] remarks = remark.split("_");
		String leadrname = remarks[2];
		return leadrname;
	}

	// 新增公司或小区
	@Transactional
	public JsonResult<JsonOrg> addfatherorg(String orgname, String orgfather, String en_orgname, String leadername,
			String phonenum, String cn_areanum, String languagecookie) {
		// 字典
		Sysdic orgsysdic = new Sysdic();
		orgsysdic.setSysdictype("0001");
		orgsysdic.setSysdicname("组织架构");
		Integer ord = null;
		if (sysdicdao.findBySysdictypeOrderBySysdicordDesc("0001").size() == 0) {
			orgsysdic.setSysdicord(1);
			orgsysdic.setSysdicvalue("1");
		} else {
			ord = sysdicdao.findBySysdictypeOrderBySysdicordDesc("0001").get(0).getSysdicord();

			ord++; // 最大值+1 设置为新值
			orgsysdic.setSysdicord(ord);
			orgsysdic.setSysdicvalue(ord.toString());
		}

		orgsysdic.setSysdicdisplay(orgname);
		orgsysdic.setSysdicdisplay_e(en_orgname);

		orgsysdic.setSysdicisdel((byte) 0);
		// 组字符串，分隔符为_
		orgsysdic.setSysdicremake(leadername + "_" + phonenum + "_" + cn_areanum);
		sysdicdao.save(orgsysdic);
		// 架构
		Org org = new Org();
		org.setOrgvalue(ord.toString());
		org.setOrgfather(orgfather);
		if (orgdao.findByOrgfatherOrderByOrgordDesc(orgfather).size() == 0) {
			org.setOrgord(1);

		} else {
			Integer orgord = orgdao.findByOrgfatherOrderByOrgordDesc(orgfather).get(0).getOrgord();

			orgord++;
			org.setOrgord(orgord);
		}
		orgdao.save(org);

		return new JsonResult<JsonOrg>(152, "新增成功", null);

	}

	// 删除公司
	@Transactional
	public JsonResult<JsonOrgMenu> deletecompany(String orgvalue) {

		// 删除下属小区，移除关系
		List<Org> areas = orgdao.findByOrgfather(orgvalue);
		if (areas.size() != 0) {
			for (int i = 0; i < areas.size(); i++) {
				Org area = areas.get(i);
				String areavalue = area.getOrgvalue();
				// 字典逻辑删除
				sysdicdao.updateSysdicisdel((byte) 1,
						sysdicdao.findBySysdictypeAndSysdicvalue("0001", areavalue).getSysdicid());
				// 关联关系删除
				orgdao.delete(area.getOrgid());
			}
		}
		// 删除公司，移除关系
		delete(orgvalue);
		return new JsonResult<JsonOrgMenu>(155, "删除公司", null);
	}

	// 删除小区
	@Transactional
	public JsonResult<JsonOrgMenu> deletecarea(String orgvalue) {

		delete(orgvalue);
		return new JsonResult<JsonOrgMenu>(156, "删除小区", null);
	}

	private void delete(String orgvalue) {
		// 字典逻辑删除
		sysdicdao.updateSysdicisdel((byte) 1, sysdicdao.findBySysdictypeAndSysdicvalue("0001", orgvalue).getSysdicid());
		// 关系删除
		orgdao.delete(orgdao.findByOrgvalue(orgvalue));
	}

}
